<?php
require_once 'config.php'; 
require_once 'Tools.class.php'; 
require_once 'User.class.php'; 
require_once 'HTML.class.php'; 
class Library{
    private $line1="-----------------\n";
    private $line2="===============\n";
	
    //借阅查询
    public function getXJCX($openid){
        $kh=User::getXYKH($openid);
        $code=Tools::getHttp(DZU_URL."library_jccx.asp?kh=0".$kh);
        $strTitle="【借阅查询结果】\n".$this->line2;
        $lib = explode("@JCCX",$code);
        for($i=1;count($lib)>$i;$i++){

            $MC = Tools::getInfo(trim($lib[$i]),"MC");
            //$TXM = Tools::getInfo(trim($lib[$i]),"TXM");
            $JCSJ = Tools::getInfo(trim($lib[$i]),"JCSJ");
            $GHSJ = Tools::getInfo(trim($lib[$i]),"GHSJ");
            $XJCS = Tools::getInfo(trim($lib[$i]),"XJCS");
            //$FPDZ = Tools::getInfo(trim($lib[$i]),"FPDZ");
            $KZH = Tools::getInfo(trim($lib[$i]),"KZH");

            $str .= "书籍名称：《".$MC."》";
            $str .= "\n借出时间：".$JCSJ;
            $str .= "\n应还时间：".$GHSJ;
            $str .= "\n续借次数：".$XJCS;
            $str .= "\n续借：<a href=\"".API_URL."weixin/web/index.php?action=libxj&opid=_OPID&kzh=".$KZH ."&mc=".$MC."\">点击此处续借</a>\n".$this->line1;
        }//for

        if($str!=""){
            $str = $strTitle.substr($str,0,-(strlen($this->line1)));
        }else{
            $str=$strTitle."您现在没有借阅的书籍!!";
        }
        return $str;
    }  

    //归还查询
    public function getGHCX($openid){
        $kh=User::getXYKH($openid);
        $code=Tools::getHttp(DZU_URL."library_ghcx.asp?kh=0".$kh);
        $strTitle="【归还查询结果】\n".$this->line2;
        $lib = explode("@GHCX",$code);
        for($i=1;$i<count($lib);$i++){

            $MC = Tools::getInfo(trim($lib[$i]),"MC");
            //$TXM = Tools::getInfo(trim($lib[$i]),"TXM");
            $JCSJ = Tools::getInfo(trim($lib[$i]),"JCSJ");
            $GHSJ = Tools::getInfo(trim($lib[$i]),"GHSJ");
            //$XJCS = Tools::getInfo(trim($lib[$i]),"XJCS");
            $FPDZ = Tools::getInfo(trim($lib[$i]),"FPDZ");
            //$KZH = Tools::getInfo(trim($lib[$i]),"KZH");

            $str .= "书籍名称：《".$MC."》";
            $str .= "\n借出时间：".$JCSJ;
            $str .= "\n归还时间：".$GHSJ;
            $str .= "\n分配地址：".$FPDZ."\n".$this->line1;
            if($i>4)break;
        }//for

        if(count($lib)>5){
            $footer=$this->line1."<a href=\"".API_URL."weixin/web/index.php?action=libgh&opid=_OPID\">点击此处查看全部</a>\n";
        }
        if($str!=""){
            $str = $strTitle.substr($str,0,-(strlen($this->line1))).$footer;
        }else{
            $str=$strTitle."您还没有归还过书籍!!";
        }
        return $str;
    }  

     //归还查询
    public function getGHCX_more($openid){
        $kh=User::getXYKH($openid);
        $code=Tools::getHttp(DZU_URL."library_ghcx.asp?kh=0".$kh);
        $lib = explode("@GHCX",$code);
        for($i=1;$i<count($lib);$i++){
            $MC = Tools::getInfo(trim($lib[$i]),"MC");
            $TXM = Tools::getInfo(trim($lib[$i]),"TXM");
            $JCSJ = Tools::getInfo(trim($lib[$i]),"JCSJ");
            $GHSJ = Tools::getInfo(trim($lib[$i]),"GHSJ");
            $XJCS = Tools::getInfo(trim($lib[$i]),"XJCS");
            $FPDZ = Tools::getInfo(trim($lib[$i]),"FPDZ");
            $KZH = Tools::getInfo(trim($lib[$i]),"KZH");
            $info.="书籍名称：".$MC.'<br />';
            $info.="借出时间：".$JCSJ.'<br />';
            $info.="归还时间：".$GHSJ.'<br />';
            $info.="续借次数：".$XJCS.'<br />';
            $info.="分配地址：".$FPDZ.'<br />';
            $str .= HTML::makeList("book1.png",$MC,$JCSJ.'—'.$GHSJ,$info);
            $info="";
        }//for
        if($str==''){
            $str=HTML::makeListNULL("没有归还记录");
        }
        $tpl=file_get_contents(API_URL."weixin/web/list.html");
        $tpl=str_replace("_FORM", '', $tpl);
        $tpl=str_replace("_TITLE", '归还查询', $tpl);
        $tpl=str_replace("_LIST", $str, $tpl);
        return $tpl;
    }  

     //图书搜索
    public function getSearch($openid,$k){
        if($k!=''){
            $code=Tools::getHttp(DZU_URL."library_sscx.asp?txtTm=".urlencode($k));
        }
        $lib = explode("@SSJG",$code);
        for($i=1;$i<count($lib);$i++){
            $XH = Tools::getInfo(trim($lib[$i]),"XH");
            $SQH = Tools::getInfo(trim($lib[$i]),"SQH");
            $ZTM = Tools::getInfo(trim($lib[$i]),"ZTM");
            $ZRZ = Tools::getInfo(trim($lib[$i]),"ZRZ");
            $CBZ = Tools::getInfo(trim($lib[$i]),"CBZ");
            $CBRQ = Tools::getInfo(trim($lib[$i]),"CBRQ");

            $info.="正题名：".$ZTM.'<br />';
            $info.="出版者：".$CBZ.'<br />';
            $info.="责任者：".$ZRZ.'<br />';
            $info.="出版日期：".$CBRQ.'<br />';
            $info.="索取号：".$SQH.'<br />';
            $str .= HTML::makeList("library2.png",$ZTM,$CBZ.' '.$CBRQ,$info);
            $info="";
        }//for
        if($str==''){
            $str=HTML::makeListNULL("没有搜索结果");
        }
        $tpl=file_get_contents(API_URL."weixin/web/list.html");
        $tpl=str_replace("_FORM", $this->getFrom($openid,$k), $tpl);
        $tpl=str_replace("_TITLE", '图书搜索', $tpl);
        $tpl=str_replace("_LIST", $str, $tpl);
        return $tpl;
    } 
    //账目查询
    public function getZMCX($openid){
        $kh=User::getXYKH($openid);
        $code=Tools::getHttp(DZU_URL."library_zmcx.asp?kh=0".$kh);
        $strTitle="【账目查询结果】\n".$this->line2;
        $lib = explode("@ZMCX",$code);
        for($i=1;$i<count($lib);$i++){

            $XH = Tools::getInfo(trim($lib[$i]),"XH");
            $RQ = Tools::getInfo(trim($lib[$i]),"RQ");
            $ZTM = Tools::getInfo(trim($lib[$i]),"ZTM");
            $KM = Tools::getInfo(trim($lib[$i]),"KM");
            $JE = Tools::getInfo(trim($lib[$i]),"JE");

            $str .= "名称：".$ZTM;
            $str .= "\n时间：".$RQ;
            $str .= "\n金额：".$KM.' '.$JE."\n".$this->line1;
        }//for

        if($str!=""){
            $str = $strTitle.substr($str,0,-(strlen($this->line1)));
        }else{
            $str=$strTitle."没有您的账目信息";
        }
        return $str;
    }  

    //图书续借
    public function setTSXJ($kzh,$oid,$mc){
        $xyk=array('112519','112579', '108992', '107837', '107889', '108991','111950','129324');
        $q="library_xj.asp";
        $kh=User::getXYKH($oid);
        for($i=0;$i<count($xyk);$i++){
            if($kh==$xyk[$i]){
                $q="library_xj2.asp";
                break;
            }
        }
        $r=Tools::getHttp(DZU_URL.$q."?kh=0".$kh.'&id='.$kzh);
        //$r=file_get_contents(DZU_URL.$q."?kh=0".$kh.'&id='.$kzh);
        $r="<br><br><br><br><b>《".$mc."》</b><br><br>".$r;
        $str=HTML::makeListNULL($r);
        $tpl=file_get_contents(API_URL."weixin/web/msg.html");
        $tpl=str_replace("_TITLE", '图书续借', $tpl);
        $tpl=str_replace("_LIST", $str, $tpl);
        return $tpl;
    }

    function getFrom($oid,$k){
        $str.='<form action="" method="get">';
        $str.='&nbsp;&nbsp;';
        $str.='<input name="txtTm" value="'.$k.'" class="search" placeholder="点击此处输入关键字" />';
        $str.='<input type="hidden" name="opid" value="'.$oid.'" />';
        $str.='<input type="hidden" name="action" value="tsjs" />';
        $str.='&nbsp;&nbsp;<input class="cbtn" type="submit" name="submit" value="查询" />';
        $str.='</form>';

        return $str;
    }
}
?>